<?php
namespace App\Http\Controllers\ApiWx;

use App\DBs\AddressDB;
use Illuminate\Http\Request;

class AddressController extends BaseController
{
    //收货地址

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new AddressDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        $wx = $request->post('wx','');
        if (!$wx) {
            return $this->toWxJson(-4);
        }
        $resDb = $this->db->getWxIndex($wx);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data'];
        return $this->toWxJson(0,$items);
    }

    /**
     * 新增
     */
    public function setAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $uname = $request->post('uname','');
        $uphone = $request->post('uphone','');
        $uprovince = $request->post('uprovince','');
        $ucity = $request->post('ucity','');
        $uarea = $request->post('uarea','');
        $ustreet = $request->post('ustreet','');
        if (!$wx || !$uname || !$uphone) {
            return $this->toWxJson(-4);
        }
        if (strlen($uphone)!=11) {
            return array('code'=>'-5','msg'=>'手机号必须11位');
        }
        $resDb = $this->db->setAdd($wx,$uname,$uphone,$uprovince,$ucity,$uarea,$ustreet);
        if ($resDb['code']!=0) {
            return array('code'=>'-6','msg'=>$resDb['msg']);
        }
        return $this->toWxJson(0);
    }

    /**
     * 选中
     */
    public function setCheck(Request $request)
    {
        $id = $request->post('id',0);
        if (!$id) {
            return $this->toWxJson(-4);
        }
        $resDb = $this->db->setCheck($id);
        if ($resDb['code']!=0) {
            return array('code'=>'-5','msg'=>$resDb['msg']);
        }
        return $this->toWxJson(0);
    }

    /**
     * 删除
     */
    public function setDel(Request $request)
    {
        $id = $request->post('id',0);
        if (!$id) {
            return $this->toWxJson(-4);
        }
        $resDb = $this->db->setDel($id);
        if ($resDb['code']!=0) {
            //return $this->toWxJson(-500);
            return array('code'=>'-5','msg'=>$resDb['msg']);
        }
        return $this->toWxJson(0);
    }
}